package com.margo.project.curator.election;

import java.util.ArrayList;
import java.util.List;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.leader.LeaderLatch;
import org.apache.curator.framework.recipes.leader.LeaderLatchListener;
import org.apache.curator.framework.recipes.leader.Participant;
import org.apache.curator.retry.ExponentialBackoffRetry;
/**
 *    https://blog.csdn.net/haizeihdj/article/details/80796227
 * @author fangxingbang
 *
 */
public class MargoLeaderLatch {
	public static void main(String[] args) throws Exception {
		List<LeaderLatch> latches = new ArrayList<LeaderLatch>();
        List<CuratorFramework> clients = new ArrayList<CuratorFramework>();
        for (int i = 0; i < 10; i++) {
            CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry(1000, 3));
            client.start();
            clients.add(client);

            final LeaderLatch leaderLatch = new LeaderLatch(client, "/master", "node-" + i, LeaderLatch.CloseMode.NOTIFY_LEADER);
            leaderLatch.addListener(new LeaderLatchListener() {
                public void isLeader() {
                    System.out.println(leaderLatch.getId() + " is a leader ");
//                    try {
//						Thread.sleep(1000);
//					} catch (InterruptedException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					} 
                    System.out.println("#################瀹氭椂浠诲姟###################");
                }

                public void notLeader() {
                    System.out.println(leaderLatch.getId() + " not a leader ");
                }
            });
            
            //leaderLatch.await();
            latches.add(leaderLatch);
        }
        for (LeaderLatch latch : latches) {
        	latch.start();
            System.out.println(latch.getId()+" elect end");
        }
        Thread.sleep(20000);
//        for (LeaderLatch latch : latches) {
//            System.out.println(latch.getId() + " leader:" + latch.getLeader() + " isLeader:" + latch.hasLeadership());
//            if(latch.hasLeadership()){
//                latch.close();
//            }
//        }
        for (LeaderLatch latch : latches) {
        	//鑾峰彇棣栭鑺傜偣
        	Participant participant=latch.getLeader();
        	if(participant.isLeader()) {
        		System.out.println(participant.getId());
        		latch.close();
        	}
        	/**
        	 * 	leaderLatch.close();
        	 * When you are through with the LeaderLatch instance, you must call close. 
        	 * This removes the instance from the leader election and releases leadership if the instance has it.
        	 *  Once leadership is released, another participating instance (if any) will be chosen as leader.
        	 */
        	
        }
        Thread.sleep(20000);
        for (CuratorFramework client : clients) {
            System.out.println(client.getData() + " is close");
            client.close();
        }
	}
}
